package com.jumi.microservice.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.utils.DelFlag;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.request.LogOutQueryRequest;
import com.jumi.microservice.entity.JmAdminChangeUserLog;
import com.jumi.microservice.entity.JmLogout;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.enums.CheckStatus;
import com.jumi.microservice.enums.CommonEnum;
import com.jumi.microservice.enums.Status;
import com.jumi.microservice.service.IJmAdminChangeUserLogService;
import com.jumi.microservice.service.IJmLogoutService;
import com.jumi.microservice.service.IJmUserService;
import com.jumi.microservice.service.PageService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/jm-logout")
@Api(value="注销申请",tags={"注销申请接口"})
public class JmLogoutController {
    @Autowired
    IJmLogoutService service;

    @Autowired
    IJmAdminChangeUserLogService logService;

    @PostMapping("/getInfo")
    @ApiOperation("获取注销申请列表")
    public TableDataInfo<JmLogout> getInfo(LogOutQueryRequest request,
                                           Integer size, Integer current){

        Page page=new Page(current,size);
        QueryWrapper wrapper= WrapperUtil.getWrapper(null,request);
        wrapper.orderByDesc("id");
        page=service.getPage(page,wrapper);
        return PageService.getDataTableInfo(page);
    }


    @Autowired
    IJmUserService userService;

    @PostMapping("/update")
    @ApiOperation("注销申请审核")
    public ResponseResult update(Integer id, CheckStatus status, String adminReason){
        JmLogout byId = service.getById(id);
        if(ObjectUtil.isNull(byId))return ResponseResult.error("未查询到相关记录!");
        Integer uid = byId.getUid();

        JmUser jmUser = userService.checkUserExist(uid);
        Long adminId = ServletUtils.getLoginId();
        String adminName = ServletUtils.getLoginName();
        LocalDateTime now = LocalDateTime.now();

        byId.setExamineTime(now);
        byId.setStatus(status);
        byId.setAdminReason(adminReason);
        byId.setAdminId(adminId);
        byId.setAdminName(adminName);

        if(status.equals(CheckStatus.已通过)){
            jmUser.setUpdateTime(LocalDateTime.now());
            jmUser.setStatus(Status.关闭);
            jmUser.setAllowLogin(CommonEnum.否);
            jmUser.setAllowAuthorize(CommonEnum.否);
            jmUser.setAllowReport(CommonEnum.否);
            jmUser.setAllowBuy(CommonEnum.否);

            JmAdminChangeUserLog log=new JmAdminChangeUserLog();
            log.setUid(byId.getUid());
            log.setType("注销");
            log.setAdminId(adminId);
            log.setAdminName(adminName);
            log.setBackup(byId.getApplyReason());
            log.setUpdateTime(LocalDateTime.now());
            log.setCreateTime(LocalDateTime.now());
            log.setDel(DelFlag.normal);
            logService.save(log);
            userService.updateById(jmUser);
        }
        service.updateById(byId);
        return ResponseResult.success();
    }
}
